/* Copyright 2001,2002,2003 NAH6
 * All Rights Reserved
 *
 * Parts Copyright DoD, Parts Copyright Starium
 *
 */
#include "resample.h"


/*************************************************************************
*
* ROUTINE
*               Resample
*
* FUNCTION
*
*               Upsample and select appropriate samples to resample
*               at fractional delay
*
* SYNOPSIS
*
*               Resample(sig_in, wsinc, q_frac_pit, out_len,
*                       out_start, int_pit, m1, m2, sig_out)
*
*
*
*   formal
*                       data    I/O
*       name            type    type    function
*       -------------------------------------------------------------------
*       sig_in          fxpt_16  i      Input signal
*       wsinc           fxpt_16  i      Hamming windowed sinc interpolating
*                                       function
*       q_frac_pit      int      i      Quantized fractional pitch
*       out_len         int      i      Length of output signal
*       out_start       int      i      Beginning of output signal
*       int_pit         int      i      Integer pitch
*       m1              int      i      Lower interpolation bound
*       m2              int      i      Upper interpolation bound
*       sig_out         fxpt_16  o      Delayed input signal
**************************************************************************/

void Resample_long(
fxpt_16 sig_in[],                               /* 15.0 format */
fxpt_16 wsinc[MAX_M1+MAX_M2+1][MAX_NFRAC],      /* 0.15 format */
int     q_frac_pit,
int     int_pit,
fxpt_16 sig_out[])                              /* 15.0 format */
{
  int   i, j;
  int64 acc;
  fxpt_16 *ps, *pw, *ps2;

  for (i= SF_LEN, ps2= sig_in; i>0; --i, ++ps2) {
    acc = 0;
    for( j= 40, ps= sig_in - int_pit - i -20+SF_LEN, pw= &wsinc[0][0]+q_frac_pit; j > 0; --j,++ps,pw+= MAX_NFRAC )
	{
      acc += (fxpt_32)*ps * *pw;
    }
    //sig_in[i] = acc >> 15;
    *ps2= fxpt_saturate16(fxpt_saturate32_round(acc,15));
  }

  for(i = SF_LEN,ps=sig_in,ps2=sig_out; i >0 ; --i,++ps,++ps2)   {
    *ps2 = *ps;
    *ps = 0;
  }
}
